//
//  MGSplitDividerView.m
//  MGSplitView
//
//  Created by Matt Gemmell on 26/07/2010.
//  Copyright 2010 Instinctive Code.
//

#import "MGSplitDividerView.h"
#import "MGSplitViewController.h"


@implementation MGSplitDividerView
@synthesize isdivide_master;
@synthesize splitViewController;
@synthesize allowsDragging;

#pragma mark -
#pragma mark Setup and teardown


- (id)initWithFrame:(CGRect)frame
{
	if ((self = [super initWithFrame:frame])) {
		
        self.userInteractionEnabled = NO;
		self.allowsDragging = NO;
		self.contentMode = UIViewContentModeRedraw; //一旦外框改變就重畫內容
	}
	return self;
}


- (void)dealloc
{
	self.splitViewController = nil;
	[super dealloc];
     
}


#pragma mark -
#pragma mark 繪圖


- (void)drawRect:(CGRect)rect{
	if (splitViewController.dividerStyle == MGSplitViewDividerStyleThin) {
	
        [super drawRect:rect];
		
	} 
    else if (splitViewController.dividerStyle == MGSplitViewDividerStylePaneSplitter) {
		
        //1.繪製漸層底色.
		CGRect bounds = self.bounds;
		CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
		CGFloat locations[2] = {0, 1};
		CGFloat components[8] = {	0.988, 0.988, 0.988, 1.0,  // light
									0.875, 0.875, 0.875, 1.0 };// dark
		CGGradientRef gradient = CGGradientCreateWithColorComponents (rgb, components, locations, 2);
		
        CGContextRef  context = UIGraphicsGetCurrentContext();//取得畫布資訊
		
        CGPoint start, end;
		
        if (isdivide_master==NO) {
            if (splitViewController.vertical) {
                // Light left to dark right.
                start = CGPointMake(CGRectGetMinX(bounds), CGRectGetMidY(bounds));
                end = CGPointMake(CGRectGetMaxX(bounds), CGRectGetMidY(bounds));
            } else {
                // Light top to dark bottom.
                start = CGPointMake(CGRectGetMidX(bounds), CGRectGetMinY(bounds));
                end = CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds));
            }
        }
        else {
            if (!splitViewController.vertical) {
                // Light left to dark right.
                start = CGPointMake(CGRectGetMinX(bounds), CGRectGetMidY(bounds));
                end = CGPointMake(CGRectGetMaxX(bounds), CGRectGetMidY(bounds));
            } else {
                // Light top to dark bottom.
                start = CGPointMake(CGRectGetMidX(bounds), CGRectGetMinY(bounds));
                end = CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds));
            } 
        }
        
      
		
        CGContextDrawLinearGradient(context, gradient, start, end, 0);
		CGColorSpaceRelease(rgb);
		CGGradientRelease(gradient);
		
		//2.繪製邊框.
		float borderThickness = 1.0;
		
       [[UIColor colorWithWhite:0.7 alpha:1.0] set];
         
        
        
        CGRect borderRect = bounds;
		
        if (isdivide_master==NO) {
            if (splitViewController.vertical) {
                
                borderRect.size.width = borderThickness;
                UIRectFill(borderRect);
                
                borderRect.origin.x = CGRectGetMaxX(bounds) - borderThickness;
                UIRectFill(borderRect);
                
            } 
            else {
                //上面那條線 
                borderRect.size.height = borderThickness; //調整高度就是僅有 厚度
                UIRectFill(borderRect);
                
                //下面那條線
                borderRect.origin.y = CGRectGetMaxY(bounds) - borderThickness; //將y的位置往下移動到?
                //borderRect.origin.y = bounds.size.height - borderThickness;   //此方法也可以
                //bounds 與frame差異 就是bounds 的xy 一定是(0,0) frame 的位置則是看此view 相對於其父的坐標軸位置
                UIRectFill(borderRect);
            }
        }        
        else {
            if (!splitViewController.vertical) {
                
                borderRect.size.width = borderThickness;
                UIRectFill(borderRect);
                
                borderRect.origin.x = CGRectGetMaxX(bounds) - borderThickness;
                UIRectFill(borderRect);
                
            } 
            else {
                //上面那條線 
                borderRect.size.height = borderThickness; //調整高度就是僅有 厚度
                UIRectFill(borderRect);
                
                //下面那條線
                borderRect.origin.y = CGRectGetMaxY(bounds) - borderThickness; //將y的位置往下移動到?
                //borderRect.origin.y = bounds.size.height - borderThickness;   //此方法也可以
                //bounds 與frame差異 就是bounds 的xy 一定是(0,0) frame 的位置則是看此view 相對於其父的坐標軸位置
                UIRectFill(borderRect);
            }            
            
        }
        if (splitViewController.vertical) {
		
             borderRect.size.width = borderThickness;
			 UIRectFill(borderRect);
			
            borderRect.origin.x = CGRectGetMaxX(bounds) - borderThickness;
			 UIRectFill(borderRect);
			
		} 
        else {
			 //上面那條線 
             borderRect.size.height = borderThickness; //調整高度就是僅有 厚度
			  UIRectFill(borderRect);
			 
             //下面那條線
              borderRect.origin.y = CGRectGetMaxY(bounds) - borderThickness; //將y的位置往下移動到?
             //borderRect.origin.y = bounds.size.height - borderThickness;   //此方法也可以
             //bounds 與frame差異 就是bounds 的xy 一定是(0,0) frame 的位置則是看此view 相對於其父的坐標軸位置
             UIRectFill(borderRect);
		}
		
		//3.繪製三條小線
		 [self drawGripThumbInRect:bounds];
	}
}

//繪製三條小線
- (void)drawGripThumbInRect:(CGRect)rect{
	float width = 9.0;
	float height;
	
    if (isdivide_master==NO) {
        if (splitViewController.vertical) {
            height = 30.0;
        } else {
            height = width;
            width = 30.0;
        }
    }    
    else {
        if (!splitViewController.vertical) {
            height = 30.0;
        } else {
            height = width;
            width = 30.0;
        }
    }
   
	
	// Draw grip in centred in rect.
	CGRect gripRect = CGRectMake(0, 0, width, height);
	gripRect.origin.x = ((rect.size.width - gripRect.size.width) / 2.0);
	gripRect.origin.y = ((rect.size.height - gripRect.size.height) / 2.0);
	
	float stripThickness = 1.0;
	UIColor *stripColor = [UIColor colorWithWhite:0.35 alpha:1.0];
	UIColor *lightColor = [UIColor colorWithWhite:1.0 alpha:1.0];
	float space = 3.0;
	
    if (isdivide_master==NO) {
        if (splitViewController.vertical) {
            gripRect.size.width = stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.x += stripThickness;
            gripRect.origin.y += 1;
            [lightColor set];
            UIRectFill(gripRect);
            gripRect.origin.x -= stripThickness;
            gripRect.origin.y -= 1;
            
            gripRect.origin.x += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.x += stripThickness;
            gripRect.origin.y += 1;
            [lightColor set];
            UIRectFill(gripRect);
            gripRect.origin.x -= stripThickness;
            gripRect.origin.y -= 1;
            
            gripRect.origin.x += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.x += stripThickness;
            gripRect.origin.y += 1;
            [lightColor set];
            UIRectFill(gripRect);
            
        } 
        else {
           
            gripRect.size.height = stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y += stripThickness;
            gripRect.origin.x -= 1;
            [lightColor set];
            UIRectFill(gripRect);
            
             
            
            gripRect.origin.y -= stripThickness;
            gripRect.origin.x += 1;
            gripRect.origin.y += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y += stripThickness;
            gripRect.origin.x -= 1;
            [lightColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y -= stripThickness;
            gripRect.origin.x += 1;
            gripRect.origin.y += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y += stripThickness;
            gripRect.origin.x -= 1;
            [lightColor set];
            UIRectFill(gripRect);
            
            
        }        
        
    }
    else {
        if (!splitViewController.vertical) {
            gripRect.size.width = stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.x += stripThickness;
            gripRect.origin.y += 1;
            [lightColor set];
            UIRectFill(gripRect);
            gripRect.origin.x -= stripThickness;
            gripRect.origin.y -= 1;
            
            gripRect.origin.x += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.x += stripThickness;
            gripRect.origin.y += 1;
            [lightColor set];
            UIRectFill(gripRect);
            gripRect.origin.x -= stripThickness;
            gripRect.origin.y -= 1;
            
            gripRect.origin.x += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.x += stripThickness;
            gripRect.origin.y += 1;
            [lightColor set];
            UIRectFill(gripRect);
            
        } 
        else {
            
            gripRect.size.height = stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y += stripThickness;
            gripRect.origin.x -= 1;
            [lightColor set];
            UIRectFill(gripRect);
            
            
            
            gripRect.origin.y -= stripThickness;
            gripRect.origin.x += 1;
            gripRect.origin.y += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y += stripThickness;
            gripRect.origin.x -= 1;
            [lightColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y -= stripThickness;
            gripRect.origin.x += 1;
            gripRect.origin.y += space + stripThickness;
            [stripColor set];
            UIRectFill(gripRect);
            
            gripRect.origin.y += stripThickness;
            gripRect.origin.x -= 1;
            [lightColor set];
            UIRectFill(gripRect);
            
            
        }  
        
        
        
    }
   
}

/*
 CGContextRef CGContext，一個ObjectiveC製作繪圖的資料型態(參照)
 CGLayerRef
 UIGraphicsGetCurrentContext
 (CGContextRef) CGBitmapContextCreate  產生bitmap形態的ContextRef
 (CGGradientRef) CGGradientCreateWithColorComponents  產生一個漸層填色
 CGContextDrawLinearGradient  套用一個漸層的繪圖填色
 CGColorSpaceCreateDeviceGray 產生裝置支援的灰階色域(類似色票)
 CGColorSpaceCreateDeviceRGB  產生裝置支援的RGB色域
 CGBitmapContextCreateImage 將CGContextRef 轉成CGImageRef
 CGContextClipToMask  對CGContextRef套用CGImageRef格式的mask
 CGContextScaleCTM  延展座標
 CGContextTranslateCTM  位移座標 
 
 CGContextSaveGState 與 CGContextRestoreGState，花了一點時間將CGContextGSave與CGContextRestoreGState弄懂，
 
 CGContextRef是一個線性的繪圖資料型態，他儲存產生繪圖的所有需要資訊。一般而言，可以從取得bitmap、layer、window、pdf等透過CoreGraphics或者UIKit的一些function取得CGContextRef，如CGBitmapContextCreate、 UIGraphicsGetCurrentContext。
 
 取得CGContextRef之後，就可以對他進行繪圖，而所有的繪圖資料都是透過一些CG的function將context一層一層的覆蓋下去，所以我們可以想成每次的修改，都是針對目前的Status進行修改。因此，原本的圖形繪圖資料，經function處理後，產生新的status，舊的圖形資料就被蓋下去由於是一層一層蓋下去的，每蓋一層，就是一個新的Status，如同油畫一樣，所以舊的資料就會被蓋到下層無法取得
 
 因此有兩個function為CGContextSaveGState 與 CGContextRestoreGState
 
 CGContextSaveGState，將目前的STATUS push一個copy到一個Stack，
 之後的修改將不會動到此STATUS，有點類似紀錄點的意思
 
 CGContextRestoreGState，從Stack pop一個Status回來，原先的status之後的修改將會被捨棄掉，可以這樣想：
 A--->B--->save--->C--->D--->E--->F--->restore--->B
 
 但並非所有修改都支援status的save，只有下表的CoreGraphic 參數支援status的save。
 注意，下表提供的path，CGContextStrokePath、CGContextStrokeRect之類的動作，是不支援Status Save的，但先前的moveTo、lineTo、color才支援Status Save 
 
 */

#pragma mark -
#pragma mark 互動


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	 
    UITouch *touch = [touches anyObject];
	if (touch) {
        
        
		CGPoint lastPt = [touch previousLocationInView:self];
		CGPoint pt = [touch locationInView:self];
		
        float offset;
       
        if (!isdivide_master) {
            offset= (splitViewController.vertical) ? pt.x - lastPt.x : pt.y - lastPt.y; 
            if (!splitViewController.masterBeforeDetail) {
                 offset = -offset;
            }        
        }
        else {
            offset= (!splitViewController.vertical) ? pt.x - lastPt.x : pt.y - lastPt.y;
            if (!splitViewController.masterBeforeDetail) {
                offset = offset;
            }            
        }
        
        //移動位置
        //splitViewController.splitPosition = splitViewController.splitPosition + offset;
        
        [splitViewController setsplitPositionaa: [splitViewController splitPositionaa:isdivide_master] + offset isdividemaster:  isdivide_master];
         
	}
}


#pragma mark -
#pragma mark Accessors and properties


- (void)setAllowsDragging:(BOOL)flag
{
	if (flag != allowsDragging) {
		allowsDragging = flag;
		self.userInteractionEnabled = allowsDragging;
	}
  
}





@end
